package com.donggua.aichebao;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import java.util.ArrayList;

/**
 * Created by fzh on 2017/8/17.
 */

public class DGPermissionUtil {
    public static final int NEED_PERMISS_VERSION = 23;
    public static final String[] PERMISSION = {
            Manifest.permission.CAMERA,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.VIBRATE
    };

    public static void grantPermission(Activity activity) {
        if(Build.VERSION.SDK_INT < NEED_PERMISS_VERSION){
            return;
        }else{

        }
        ArrayList<String> permissionList = new ArrayList<String>();
        for(int i=0;i<PERMISSION.length;i++){
            int permissionCheck =  0;
            permissionCheck = permissionCheck + ContextCompat.checkSelfPermission(activity, PERMISSION[i]);
            if(permissionCheck !=  PackageManager.PERMISSION_GRANTED){
                permissionList.add(PERMISSION[i]);
            }
        }
        int size = permissionList.size();
        if(size > 0) {
            String[] perList = new String[size];
            for (int i = 0; i < size; i++) {
                perList[i] = permissionList.get(i);
            }
            ActivityCompat.requestPermissions(activity,
                    perList, 1);
        }

    }
}
